var uuid = require('node-uuid');
var promise = require('bluebird');
var math = require('mathjs');
var moment = require('moment');
var fs = promise.promisifyAll(require('fs-extra'));
var winston = require('winston');

module.exports = {
	/**
	 * Ensures that a directory is empty. Deletes directory contents if the
	 * directory is not empty. If the directory does not exist, it is created.
	 * The directory itself is not deleted.
	 */
	cleanDir : function(dir, callback) {
		return fs.emptyDirAsync(dir, callback);
	},
	/**
	 * Removes a file or directory. The directory can have contents. Like rm
	 * -rf.
	 */
	deleteDir : function(dir, callback) {
		return fs.removeAsync(dir, callback);
	},
	createFile : function(dir, callback) {
		return fs.ensureFileAsync(dir, callback);
	},
	createDir : function(dir, callback) {
		return fs.ensureDirAsync(dir, callback);
	},
	writeJson : function(file, object, options, callback) {
		return fs.writeJsonAsync(file, object, options, callback);
	},
	readJson : function(file, options, callback) {
		return fs.readJsonAsync(file, options, callback);
	},
	generateUUID : function(callback) {
		return uuid.v4();
	},
	formatFloat : function(s, n, callback) {
		n = n > 0 && n <= 20 ? n : 2;
		var s = parseFloat((s + "").replace(/[^\d\.-]/g, "")).toFixed(n) + "";
		return promise.resolve(s).asCallback(callback);
	},
	formatFloat : function(s, callback) {
		var s = parseFloat((s + "").replace(/[^\d\.-]/g, "")).toFixed(2) + "";
		return promise.resolve(s).asCallback(callback);
	},
	generateOrderNo : function() {
		return math.randomInt(1000, 9999) + "" + moment().valueOf() + "";
	}
}
